<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px"
				 label-position="left">
			<el-form-item label=全称 prop="name">
				<el-input v-model="form.name" placeholder="请输入全称" clearable></el-input>
			</el-form-item>
			<el-form-item label=简称 prop="simple_name">
				<el-input v-model="form.simple_name" placeholder="请输入简称" clearable></el-input>
			</el-form-item>
			<el-form-item label=联系人 prop="tel_name">
				<el-input v-model="form.tel_name" placeholder="请输入联系人" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系方式" prop="tel_phone">
				<el-input v-model="form.tel_phone" maxlength="11" placeholder="请输入联系方式" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系地址" prop="address">
				<el-input v-model="form.address" placeholder="请输入收货地址" clearable></el-input>
			</el-form-item>
			<el-form-item label="职位" prop="role_name">
				<el-input v-model="form.role_name" placeholder="请输入职位" clearable></el-input>
			</el-form-item>
			<el-form-item label="分级" prop="class_name">
				<el-radio-group v-model="form.class_name" class="ml-4">
					<el-radio label="A" size="large">A</el-radio>
					<el-radio label="B" size="large">B</el-radio>
					<el-radio label="C" size="large">C</el-radio>
					<el-radio label="D" size="large">D</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="类别" prop="type">
				<el-radio-group v-model="form.type" class="ml-4">
					<el-radio label="诊所" size="large">诊所</el-radio>
					<el-radio label="连锁" size="large">连锁</el-radio>
					<el-radio label="单店" size="large">单店</el-radio>
					<el-radio label="医院" size="large">医院</el-radio>
					<el-radio label="商业公司" size="large">商业公司</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-switch v-model="form.status" :active-value="true" :inactive-value="false"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
// import roleSelect from "@/components/system/roleSelect";
export default {
	emits: ['success', 'closed', 'reloadData'],
	// components:{roleSelect},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增客户',
				edit: '修改客户信息',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			// 职位select数据
			employeeeList:[],
			//表单数据
			form: {
				id: "",
				name: "",//全称
				simple_name: "",//简称
				tel_name:'',//联系人
				tel_phone:'',//联系方式
				address:'',//收货地址
				role_name:'',//职位
				class_name:'',//分级
				type:'',//类别
				status: true,//状态
			},
			// 修改密码
			changePassword: '',
			//验证规则
			rules: {
				name: [
					{required: true, message: '请输入全称'}
				],
				simple_name: [
					{required: true, message: '请输入简称'}
				],
				tel_name: [
					{required: true, message: '请输入联系人'}
				],
				tel_phone: [
					{required: true, message: '请输入联系方式'}
				],
				address: [
					{required: true, message: '请输入收货地址'}
				],
				role_name: [
					{required: true,message: '请输入职位'}
				],
				class_name: [
					{required: true,message: '请输入选择分级'}
				],
				type: [
					{required: true,message: '请输入选择类型'}
				],
				status: [
					{required: true,message: '请输入选择状态'}
				],
			}
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let res;
					if (this.mode === 'add') {
						res = await this.$API.company.CompanyCustomer.add.post(this.form);
					} else if (this.mode === 'edit') {
						// 是否修改了密码
						res = await this.$API.company.CompanyCustomer.edit.post(this.form);
					}
					this.isSaveing = false;
					if (res.code === 0) {
						this.$emit('reloadData')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
			this.form.name = data.name
			this.form.simple_name = data.simple_name
			this.form.tel_name = data.tel_name
			this.form.tel_phone = data.tel_phone
			this.form.address = data.address
			this.form.role_name = data.role_name
			this.form.class_name = data.class_name
			this.form.type = data.type
			this.form.status = data.status === 1 ? true : false
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
